﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MarioArena.World;

namespace MarioArena.HUD
{
    public class TimeDisplay
    {
        private const float transparency = 0.4f; //0 = solid, 1 = transparent
        private const int marginTop = 30; //Space between time display and top border.
        private const int marginRight = 200; //Space between left side of time display and right border.
        private bool started = false;
        private Color backgroundColor = Color.Black;
        private Color textColor = Color.White;

        private int screenWidth;
        private int screenHeight;
        private int positionX;

        private Texture2D backgroundTexture;
        private SpriteFont font;

        private long startTime;
        private int matchDurationInSeconds;
        private int passedTimeInSeconds;
        private string timeString;

        public void LoadContent()
        {
            backgroundTexture = new Texture2D(GameManager.GraphicsDevice, 1, 1);
            backgroundTexture.SetData(new[] { Color.White });
            font = GameManager.Content.Load<SpriteFont>("Fonts/Time");

            screenWidth = GameManager.GraphicsDevice.Viewport.Width;
            screenHeight = GameManager.GraphicsDevice.Viewport.Height;

            //200 pixels from the right border.
            this.positionX = screenWidth - marginRight;
        }

        public void Update(GameTime gameTime)
        {
            if (started)
            {
                int previousTimeInSeconds = this.passedTimeInSeconds;
                this.passedTimeInSeconds = (int)(( DateTime.Now.Ticks - this.startTime) / 10000000f);

                //only update time if a second has passed
                if (this.passedTimeInSeconds > previousTimeInSeconds)
                {
                    int time = this.matchDurationInSeconds - this.passedTimeInSeconds;
                    if (time < 0)
                        time = 0;
                    this.timeString = BuildTimeString(time);
                }
            }
        }

        /// <summary>
        /// Builds a single string containing the time in hours, minutes and seconds.
        /// </summary>
        /// <param name="timeInSeconds"></param>
        /// <returns></returns>
        private string BuildTimeString(int timeInSeconds)
        {
            string result = "";
            //Create time string
            int seconds = timeInSeconds;
            int minutes = seconds / 60;
            seconds -= (minutes * 60);
            int hours = minutes / 60;
            minutes -= (hours * 60);
            string minutesAsString = beautifyDigit(minutes.ToString());
            string secondsAsString = beautifyDigit(seconds.ToString());

            result = minutesAsString + ":" + secondsAsString;
            if (hours > 0)
                result = hours + ":" + result;

            return result;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            //Draw background
            //spriteBatch.Draw(backgroundTexture, this.bounds, this.backgroundColor * transparency);
            if(this.timeString != null)
                spriteBatch.DrawString(font, timeString, new Vector2(this.positionX, 0), textColor);
        }
        
        /// <summary>
        /// Beautifies digits, adding zeros in front of minutes of seconds that show only one digit.
        /// </summary>
        /// <param name="digit"></param>
        /// <returns></returns>
        private string beautifyDigit(string digit)
        {
            string result = digit;
            if (result.Length == 1)
                result = "0" + result;
            return result;
        }

        /// <summary>
        /// Starts the time display, resetting the time to the duration of the match.
        /// </summary>
        /// <param name="matchDurationInSeconds"></param>
        public void StartTime(int matchDurationInSeconds)
        {
            this.matchDurationInSeconds = matchDurationInSeconds;
            this.timeString = BuildTimeString(matchDurationInSeconds);
            this.startTime = DateTime.Now.Ticks;
            this.started = true;
        }

        /// <summary>
        /// Stops the time display.
        /// </summary>
        public void StopTime()
        {
            this.started = false;
        }
    }
}
